package com.xxtx.trade.web.userCenter;

import com.xxtx.trade.common.util.model.Result;
import com.xxtx.trade.sysUser.dto.TradeSysRoleDto;
import com.xxtx.trade.sysUser.service.TradeSysRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/xxtx/sysRole")
public class SysRoleController {
    private static final Logger LOGGER = LoggerFactory.getLogger(SysRoleController.class);


    @Autowired
    private TradeSysRoleService tradeSysRoleService;

    /**
     * 查看角色列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result roleList(@RequestBody TradeSysRoleDto tradeSysRoleDto) {
        return tradeSysRoleService.roleList(tradeSysRoleDto);
    }

    /**
     * 新增角色关系
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Result inserRole(@RequestBody TradeSysRoleDto tradeSysRoleDto) {
        Result<Object> result = new Result<>();
        try {
            result = tradeSysRoleService.inserRole(tradeSysRoleDto);
        } catch (Exception e) {
            LOGGER.info("增加/修改 角色失败，失败信息为{}", tradeSysRoleDto);
            e.printStackTrace();
            result.setMessage("查看失败，请稍后再试");
            result.setCode(400);
        }
        return result;
    }

    /**
     * 更改z状态
     */
    @RequestMapping(value = "/update/state", method = RequestMethod.POST)
    public Result updateRoleState(@RequestBody TradeSysRoleDto tradeSysRoleDto) {
        return tradeSysRoleService.updateRoleState(tradeSysRoleDto);
    }

    /**
     * 根据用户id  查看是否勾选角色
     */
    @RequestMapping(value = "/user/role", method = RequestMethod.POST)
    public Result selectRoleByUserId(@RequestBody TradeSysRoleDto tradeSysRoleDto) {
        return tradeSysRoleService.selectRoleByUserId(tradeSysRoleDto);
    }

    /**
     * 更改角色关系
     */
    @RequestMapping(value = "/update/role", method = RequestMethod.POST)
    public Result updateRole(@RequestBody TradeSysRoleDto tradeSysRoleDto) {
        Result<Object> result = new Result<>();
        try {
            result = tradeSysRoleService.updateRole(tradeSysRoleDto);
        } catch (Exception e) {
            result.setCode(400);
            result.setMessage("操作失败");
        }
        return result;
    }
}
